package org.ithang.tools.timer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ithang.application.btRecord.bean.BtRecord;
import org.ithang.application.btRecord.service.BtRecordService;
import org.ithang.tools.eth.ETHInfo;
import org.ithang.tools.eth.ETHUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;


/**
 * 检查挖矿中的提币交易状态
 * @author zyt
 *
 */
@JobHandler(value="btRecordHandler")
@Component
public class BtRecordHandler extends IJobHandler {

	@Autowired
	private BtRecordService btRecordService;
	
	
	@Override
	public ReturnT<String> execute(String param) throws Exception {
		XxlJobLogger.log("开始查询挖矿中提币记录状态开始");
		Map<String,Object> conditions=new HashMap<>();
		conditions.put("result", 4);
		List<BtRecord> bts=btRecordService.query(conditions);
		if(null!=bts&&!bts.isEmpty()){
			for(BtRecord bt:bts){
				ETHInfo ei=ETHUtil.detail(bt.getTxhash());
				if(null!=ei){
						if(200==ei.getStatus()){
					       String status=ei.getData().get("status");
					       if("1".equals(status)){
       					      bt.setResult(6);
       					      btRecordService.withdrawSuccess(bt);
					       }
						}else{
							   String status=ei.getData().get("status");
						       if("1".equals(status)){
	       					      bt.setResult(6);
	       					      btRecordService.withdrawSuccess(bt);
						       }
						}
				}
			}
		}
		XxlJobLogger.log("开始查询挖矿中提币记录状态结束");
		
		return SUCCESS;
	}

}
